

class LampShape:
    R = 15
    def __init__(self, canvas, x, y, name):
        self.canvas = canvas
        self.x = x
        self.y = y
        self.rectid = self.canvas.create_rectangle(x - self.R, y - self.R, x + self.R, y + self.R, fill="black", outline="DarkGray", tags="lamp")
        self.name = name    # 在序列化时保存
        self.textid = None
        self.connectto = [] # 移动时使用
        self.connectfrom = [] # 移动时使用
        self.set_name(name)

    def set_name(self, name):
        self.name = name
        if name.startswith("__"):
            name = ""
        elif "__" in name:
            name = name.split("__")[0]
        else:
            pass    # name = name
        if self.textid:
            self.canvas.delete(self.textid)
        x1, y1, x2, y2 = self.canvas.coords(self.rectid)
        self.textid = self.canvas.create_text(x1 + self.R, y1 + self.R * 2 + 10, text=name, fill="DarkGray", font=self.ft)
        

    def del_shape(self):
        self.canvas.delete(self.rectid)
        if self.textid:
            self.canvas.delete(self.textid)

    def move(self, x, y):
        dx, dy = x - self.x, y - self.y
        self.x, self.y = x, y
        self.canvas.move(self.rectid, dx, dy)
        if self.textid:
            self.canvas.move(self.textid, dx, dy)

    


class ConnectShape: # 有向的连接。双向连接 通过错开1个像素解决。不再画两段。
    def __init__(self, canvas, startlamp, endlamp, color): 
        self.canvas = canvas
        self.startlamp, self.endlamp = startlamp, endlamp
        self.startx, self.starty = startlamp.x, startlamp.y
        self.endx, self.endy = endlamp.x, endlamp.y
        self.startlamp, self.endlamp = startlamp, endlamp
        startx, starty, midx, midy, endx, endy = self.calc()
        self.lineid = self.canvas.create_line(midx, midy, endx, endy, fill=color)
        self.arrowid = self.canvas.create_line(startx, starty, midx, midy, arrow="last", arrowshape="10 14 5", fill=color)
        self.canvas.tag_lower(self.lineid)
        self.canvas.tag_lower(self.arrowid)
        
    def calc(self):
        startx, starty, endx, endy = self.startx, self.starty, self.endx, self.endy
        vx, vy = endx - startx, endy - starty
        len = (vx**2 + vy **2)**0.5
        e1x, e1y = vx / len, vy / len
        e2x, e2y = e1y, -e1x
        startx, starty, endx, endy = startx +e2x, starty +e2y, endx +e2x, endy +e2y
        midx, midy = (startx*(1/3) + endx*(2/3)), (starty*(1/3) + endy*(2/3))
        return startx, starty, midx, midy, endx, endy

    def del_shape(self):
        self.canvas.delete(self.lineid)
        self.canvas.delete(self.arrowid)

    def move_start(self, x, y):
        self.startx, self.starty = x, y
        startx, starty, midx, midy, endx, endy = self.calc()
        self.canvas.coords(self.arrowid, startx, starty, midx, midy)
        self.canvas.coords(self.lineid, midx, midy, endx, endy)

    def move_end(self, x, y):
        self.endx, self.endy = x, y
        startx, starty, midx, midy, endx, endy = self.calc()
        self.canvas.coords(self.arrowid, startx, starty, midx, midy)
        self.canvas.coords(self.lineid, midx, midy, endx, endy)